#include <iostream>
#include <unistd.h>
#include <signal.h>

void printPending(const sigset_t& pending)
{
    for(int signo = 31; signo > 0; signo--)
    {
        if(sigismember(&pending, signo) == true)
            std::cout << "1";
        else 
            std::cout << "0";
        if(signo % 4 == 0) 
            std::cout << " ";
        if(signo % 8 == 0)
            std::cout << "  ";
    }
    std::cout << "\n";
}

void handler(int signo)
{
    std::cout << "信号递达: 2" << std::endl;
}

int main()
{
    signal(2, handler);

    sigset_t block, oblock;
    sigemptyset(&block);
    sigemptyset(&oblock);
    sigaddset(&block, 2);
    sigprocmask(SIG_BLOCK, &block, &oblock);

    sigset_t pending;
    sigemptyset(&pending);

    int cnt = 0;
    while(true) 
    {
        sigpending(&pending);
        printPending(pending);

        cnt++;
        if(cnt == 5)
        {
            std::cout << "2号信号解除屏蔽，准备递达" << std::endl;
            sigprocmask(SIG_SETMASK, &oblock, nullptr);
        }

        sleep(1);
    }

    return 0;
}